package com.dmall.chapter03.section03;

/**
 * 加缓存的方式：自底向上的遍历
 * 时间复杂度：O(n)
 * 空间复杂度：O(n)
 * @author xiao1.wang
 * @date 2020-12-01 10:54:21
 */
public class Leetcode70V3 {

    public int climbStairs(int n) {
        if (n <= 2) {
            return n;
        }
        int[] cache = new int[n];
        cache[0] = 1;
        cache[1] = 2;
        for (int i = 2; i < n; i++) {
            cache[i] = cache[i - 1] + cache[i - 2];
        }
        return cache[n - 1];
    }

    public static void main(String[] args) {
        Leetcode70V3 leetcode70 = new Leetcode70V3();
        System.out.println(leetcode70.climbStairs(3));
    }
}
